package mysql

import (
	"video-hub/videorpc/videorpc"

	"gorm.io/gorm"
)

type Collection struct {
	gorm.Model
	VideoId       int64  `gorm:"type:int(11);not null;comment:视频ID"`
	UserId        int64  `gorm:"type:int(11);not null;comment:用户ID"`
	CollectStatus string `gorm:"type:varchar(255);default:0;not null;comment:收藏状态,0-已收藏，1-已取消"`
}

func (c *Collection) FindId(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("collections.user_id = ? or collections.video_id = ? ", id, id2).First(c).Error
}
func (c *Collection) Add(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collection) DelCOl(db *gorm.DB, id int64) error {
	return db.Debug().Where("collections.id = ?", id).Delete(c).Error
}

func CollectList(db *gorm.DB, in *videorpc.CollectListRequest) []*videorpc.CollectList {
	var colList []*videorpc.CollectList
	db.Debug().Model(&Collection{}).Select("users.id as user_id,users.nick_name,collections.created_at," +
		"videos.id as video_id ,videos.video_title,videos.play_num,videos.cover_link").
		Joins("LEFT JOIN users ON collections.user_id = users.id").
		Joins("LEFT JOIN videos ON collections.video_id = videos.id").Order("collections.created_at desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&colList)
	return colList
}
